package com.bsi.core.datasource;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.servlet.Servlet;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author liusongs
 * content:抽取通用配置到common-core工程中
 */

@ConditionalOnClass(DruidConfigProperties.class)
@Import(DruidConfigProperties.class)
public class DruidConfig {

    private static final Logger logger = LogManager.getLogger(DruidConfig.class);

    /**
     * 配置数据源
     *
     * @param druidConfigProperties 数据库配置文件
     * @return
     */
    @Bean
    public DataSource druidDataSource(DruidConfigProperties druidConfigProperties) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName(druidConfigProperties.getDriverClassName());
        druidDataSource.setUrl(druidConfigProperties.getUrl());
        druidDataSource.setUsername(druidConfigProperties.getUsername());
        druidDataSource.setPassword(druidConfigProperties.getPassword());
        druidDataSource.setInitialSize(druidConfigProperties.getInitialSize());
        druidDataSource.setMinIdle(druidConfigProperties.getMinIdle());
        druidDataSource.setMaxActive(druidConfigProperties.getMaxActive());
        druidDataSource.setMaxWait(druidConfigProperties.getMaxWait());
        druidDataSource.setTimeBetweenEvictionRunsMillis(druidConfigProperties.getTimeBetweenEvictionRunsMillis());
        druidDataSource.setMinEvictableIdleTimeMillis(druidConfigProperties.getMinEvictableIdleTimeMillis());
        druidDataSource.setValidationQuery(druidConfigProperties.getValidationQuery());
        druidDataSource.setTestWhileIdle(druidConfigProperties.isTestWhileIdle());
        druidDataSource.setTestOnBorrow(druidConfigProperties.isTestOnBorrow());
        druidDataSource.setTestOnReturn(druidConfigProperties.isTestOnReturn());
        druidDataSource.setPoolPreparedStatements(druidConfigProperties.isPoolPreparedStatements());
        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(druidConfigProperties.getMaxPoolPreparedStatementPerConnectionSize());
        druidDataSource.setConnectionProperties(druidConfigProperties.getConnectionProperties());
//        druidDataSource.setUseGlobalDataSourceStat();
        try {
            druidDataSource.setFilters(druidConfigProperties.getFilters());
            druidDataSource.init();
        } catch (SQLException e) {
            logger.error("druidDataSource init error", e);
        }

        return druidDataSource;
    }


    /**
     * 配置druid连接池的监控页面
     *
     * @param druidConfigProperties
     * @return
     */
    @Bean
    public ServletRegistrationBean druidServlet(DruidConfigProperties druidConfigProperties) {
        // StatViewServlet德鲁伊的访问servlet
        ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet());
        registrationBean.addUrlMappings("/druid/*");
        //是否能够重置数据 禁用HTML页面上的“Reset All”功能
        // IP白名单，不设默认都可以
        // registrationBean.addInitParameter("allow", "192.168.2.25,172.16.20.1");
        // IP黑名单(共同存在时，deny优先于allow)
        //registrationBean.addInitParameter("deny", "192.168.1.100");
        registrationBean.addInitParameter("resetEnable", "false");
        // 配置登陆用户名密码(控制台管理用户)
        registrationBean.addInitParameter("loginUsername", druidConfigProperties.getLoginUsername());
        registrationBean.addInitParameter("loginPassword", druidConfigProperties.getLoginPassword());
        return registrationBean;
    }

    /**
     * 配置druid连接池的Filter
     *
     * @param druidConfigProperties
     * @return
     */
    @Bean
    public FilterRegistrationBean druidFilter(DruidConfigProperties druidConfigProperties) {
        FilterRegistrationBean<WebStatFilter> filterRegistrationBean = new FilterRegistrationBean<>(new WebStatFilter());
        // 添加拦截路径
        filterRegistrationBean.addUrlPatterns("/*");
        // 添加不需要过滤的uri
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        return filterRegistrationBean;
    }

}
